/**
 * @description 素材管理列表
 * @author 韩
 */

const { randomUUID } = require("crypto")
const dayjs = require("dayjs")
const model = require("../../../model/system/materialManager/materialManager.MODEL")
const { getFilter } = require("../../../utils/setFilter")

// 创建一个内容
const controller_save = async ({
    id = undefined,
    createDate = undefined,
    fileType = undefined,
    path = undefined,
    uploader = undefined,
    fileName = undefined,
    size = undefined
}) => {
    const params = {
        id,
        createDate,
        fileType,
        path,
        uploader,
        fileName,
        size
    }
    const res = await model.create({
        ...params
    })
    if (res) {
        const { id,
            createDate,
            fileType,
            path,
            uploader,
            fileName,
            size } = res

        return {
            code: 0,
            message: "保存成功",
            data: {
                id,
                createDate,
                fileType,
                path,
                uploader,
                fileName,
                size
            }
        }
    }
    return {
        status: "2000",
        message: "失败",
    }
}




// 查询列表 
const controller_find = async ({ fileName = null, fileType = null, current, pageSize, }) => {
    try {
        let filter = {
            fileName: fileName && { $regex: eval("/" + fileName + "/i") },//名称模糊查询
            fileType: fileType && { $regex: eval("/" + fileType + "/i") },//名称模糊查询
        }
        filter = getFilter(filter)
        const list = await model.find(filter).sort({ createDate: -1 }).skip((current - 1) * pageSize).limit(pageSize).lean()//查询数据
        const totalCount = await model.find(filter).countDocuments()//查询数据总数
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障"
        }
    }
}


/**@description 根据id查详情 */
const controller_get = async ({ id }) => {
    const res = await model.findOne({
        id
    })

    console.log("菜单详情", res);
    if (res) {
        const { id, parentId, menuName, menuType, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status } = res;

        return {
            code: 0,
            message: "查询成功",
            data: {
                id, parentId, menuName, menuType, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status
            }
        }
    } else {
        return {
            status: '01',
            message: "未找到对应菜单数据"
        }
    }
}

// 删除
const controller_delete = async ({ ids = [] }) => {
    const deleteResult = await model.deleteMany({
        id: { $in: ids }
    });

    if (deleteResult.deletedCount) {
        return {
            status: '0000',
            message: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            status: '0010',
            message: `删除失败`
        }
    }

}
module.exports = {
    controller_save,
    controller_find,
    controller_get,
    controller_delete,
}